<?php

namespace app\common\service;

/**
 * 发起HTTP请求
 * Class Request
 * @package app\common\service
 */
class Request
{
    /**
     * 请求路径
     * @var string
     */
    private $url;

    /**
     * 请求参数，数组部位空，表示为POST请求
     * @var array
     */
    private $params = [];

    /**
     * 请求头
     * @var array
     */
    private $header = [
        'Accept-Language'   => 'zh-CN',
        'Connection'        => 'Keep-Alive',
        'Cache-Control'     => 'no-cache',
        'Content-Type'      => 'application/json;charset=UTF-8',
    ];

    /**
     * 请求超时时间
     * @var int
     */
    private $expire = 0;

    /**
     * 请求伪造包头参数
     * @var array
     */
    private $extend = [];

    /**
     * 设置请求路径
     * @param string $url
     * @return $this
     */
    public function url(string $url)
    {
        $this->url = $url;
        return $this;
    }

    /**
     * 设置请求参数
     * @param array $params
     * @return $this
     */
    public function params(array $params){
        $this->params = array_merge($this->params, $params);
        return $this;
    }

    /**
     * 设置请求头
     * @param array $header
     * @return $this
     */
    public function header(array $header){
        $this->header = array_merge($this->header, $header) ;
        return $this;
    }

    /**
     * 设置超时时间
     * @param int $expire
     * @return $this
     */
    public function expire(int $expire){
        $this->expire = $expire;
        return $this;
    }

    /**
     * 设置额外伪造包头参数
     * @param array $extend
     * @return $this
     */
    public function extend(array $extend){
        $this->extend = array_merge($this->extend, $extend);;
        return $this;
    }

    /**
     * 发起请求
     * @return mixed
     */
    public function exec(){
        $_curl = curl_init();
        // $params不为空，就是POST方法
        if (!empty($this->params)) {
            curl_setopt($_curl, CURLOPT_POST, true);
            curl_setopt($_curl, CURLOPT_POSTFIELDS, $this->params);
        }
        // SSL证书
        if (substr($this->url, 0, 8) == 'https://') {
            curl_setopt($_curl, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($_curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        }
        // header信息
        $_header = [];
        foreach ($this->header as $k=>$v){
            $_header[] = "{$k}:{$v}";
        }
        curl_setopt($_curl, CURLOPT_URL, $this->url);
        curl_setopt($_curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($_curl, CURLOPT_USERAGENT, 'API PHP CURL');
        curl_setopt($_curl, CURLOPT_HTTPHEADER, $_header);
        if ($this->expire > 0) {
            curl_setopt($_curl, CURLOPT_TIMEOUT, $this->expire); // 处理超时时间
            curl_setopt($_curl, CURLOPT_CONNECTTIMEOUT, $this->expire); // 建立连接超时时间
        }
        // 额外的配置
        if (!empty($this->extend)) {
            curl_setopt_array($_curl, $this->extend);
        }
        $result['result'] = curl_exec($_curl);
        $result['code']   = curl_getinfo($_curl, CURLOPT_STDERR);
        $result['info']   = curl_getinfo($_curl);
        if ($result['result'] === false) {
            $result['result'] = curl_error($_curl);
            $result['code']   = -curl_errno($_curl);
        }
        curl_close($_curl);
        return $result;
    }
}